<?php
require_once 'Models/Cms/Dao/CmsBaseDao.php';

class CmsArticleDao extends CmsBaseDao {
	private static $tableName = 'cms_article';
	
	public function add($fields) {
		if (!is_array($fields)) return 0;
		return $this->queryHelper()->insert(self::$tableName, $fields);
	}
	
	public function update($id, $fields) {
		if ($id <= 0 || !is_array($fields)) return 0;
		return $this->queryHelper()->update(self::$tableName, $fields, 'id=?', array($id));
	}
	
	public function get($id) {
		return $this->queryHelper()->get(self::$tableName, null, 'id=?', array($id));
	}
	
	public function delete($id) {
		return $this->queryHelper()->delete(self::$tableName, 'id=?', array($id));
	}
	
	public function findInPage($page, $perpage, $categoryIds = array()) {
		if ($page <= 0 || $perpage <= 0) return array();
		$selection = $categoryIds ? 'category_id IN ('. $this->getImplodeString($categoryIds) .')' : '';
		return $this->queryHelper()->query(self::$tableName, null, $selection, null, null, null, 'create_at DESC', $perpage, $this->getOffset($page, $perpage));
	}
}
